import {trace} from './config';
import {getPageRandom, genSignNonce} from './util.js'
import {sendTraceData} from './trace';
import store from '@/store/index.js';
/*
	第一次打开的时候走创建页面，走切换页面的时候 就是 __prandmo 有值的话直接return 
	没有值得话给一个__prandmo  
	然后切换页面切换到其他页面时候  __prandmo有值了 
	其他页面先把旧的值放到__lastPrandom中，然后 给上一个结束时间
	然后创建一个新的__prandmo 然后创建一个新的 
*/
export default {
	data() {
		return {
			__prandom: '',
			__currentPage: '',
			__recordData: {},
			__loadTime: 0,
			__lastPrandom:'',
		}
	},
    created() {
		if (trace) {
	
			let pages = getCurrentPages();
			let tabBarInd = pages[pages.length - 1].$page.meta.tabBarIndex;

			if (store.state.vuex_user == ''){
				return
			}
			if( (this.__prandom != '' &&tabBarInd ==0 ) || tabBarInd !=0 ) {
				return 
			}
		
			let route = pages[pages.length - 1].route || '';
			let options = pages[pages.length - 1].$page.options;
			let loadTime = new Date().getTime();
			let recordData = this.__recordData;
			
			this.__loadTime = loadTime;
			this.__currentPage = '/' + route;
			recordData = Object.assign({}, recordData, options || {});
			let prandom = getPageRandom([
				genSignNonce(),
				this.__currentPage,
				this.__loadTime
			]);
			store.commit('updatetabBarMixin', prandom)
			this.__prandom = prandom;
			this.__recordData = recordData;
			sendTraceData({
				url: this.__currentPage,
				lid: prandom,
				act: 'start',
				time: loadTime,
				args: JSON.stringify(recordData),
			});
		}
    },
    mounted() {
		if (this.__prandom != '') {
			let endTime = new Date().getTime();
			sendTraceData({
			 	lid: this.__prandom,
			 	act: 'end',
			 	time: endTime,
			});
		}
      // console.log(2, '页面加载完成');
    },
  //   activated() {
	// 	if (trace) {
	// 		let pages = getCurrentPages();
	// 		let route = pages[pages.length - 1].route || '';
	// 		let options = pages[pages.length - 1].$page.options;
	// 		let loadTime = new Date().getTime();
	// 		let recordData = this.__recordData;
	// 		this.__loadTime = loadTime;
	// 		this.__currentPage = '/' + route;
	// 		recordData = Object.assign({}, recordData, options || {});
	// 		let prandom = getPageRandom([
	// 			genSignNonce(),
	// 			this.__currentPage,
	// 			this.__loadTime
	// 		]);
	// 		this.__prandom = prandom;
	// 		this.__recordData = recordData;
	// 		// sendTraceData({
	// 		// 	url: this.__currentPage,
	// 		// 	lid: prandom,
	// 		// 	act: 'start',
	// 		// 	time: loadTime,
	// 		// 	args: JSON.stringify(recordData),
	// 		// });
	// 	}
  //       console.log(3, '页面进入');
  //   },
    // onUnmounted() {
	// 		console.log("这是触发 离开的时候的  onUnmounted  onUnmountedLog  mixmin")
	// 	if (this.__prandom != '') {
	// 		let leaveTime = new Date().getTime();
	// 		sendTraceData({
	// 			lid: this.__prandom,
	// 			act: 'leave',
	// 			time: leaveTime,
	// 		});
	// 	}
    // },
	onTabItemTap(item) {
		let pages = getCurrentPages();
		let options = pages[pages.length - 1].$page.options;
		let recordData = this.__recordData;
		recordData = Object.assign({}, recordData, options || {});
		this.__recordData = recordData;
		let prandom = getPageRandom([
			genSignNonce(),
			item.pagePath,
			this.__loadTime
		]);
		let lastPrandom = store.state.tabBarMixin
		store.commit('updatetabBarMixin', prandom)

		if(lastPrandom != ''){
			let leaveTime = new Date().getTime();
			sendTraceData({
				lid: lastPrandom,
				act: 'leave',
				time: leaveTime,
			});
		}
		let loadTime = new Date().getTime();
		this.__loadTime = loadTime;
		sendTraceData({
			url: this.__currentPage,
			lid: prandom,
			act: 'start',
			time: loadTime,
			args: JSON.stringify(recordData),
		});

	},
    destroyed() {
	
		if (this.__prandom != '') {
			let leaveTime = new Date().getTime();
			sendTraceData({
				lid: this.__prandom,
				act: 'leave',
				time: leaveTime,
			});
		}
        console.log(4, '页面销毁');
    }
}